package com.dashuai.modbus.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author QM
 * @date 2023/7/28
 */
@Slf4j
@ConfigurationProperties(prefix = TCPMasterProperties.MODBUS_TCP_MASTER_PREFIX)
@Data
public class TCPMasterProperties {

    // TODO 暂时只做一个 TCP 连接类型，其他后续在做，RTU，到时候可以在新增一个类型项，叫 连接类型 0=串口、1=网口等等

    public static final String MODBUS_TCP_MASTER_PREFIX = "modbus.tcp.master";

    private List<String> ips = new ArrayList<>();

    private List<Integer> ports = new ArrayList<>();

    private String defaultIp = getLocalHost();

    private int defaultPort = 502;


    private String getLocalHost() {

        String hostAddress = "127.0.0.1";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.warn("The default value is 127.0.0.1");
        }
        return hostAddress;
    }


    public TCPMasterProperties() {
    }

    public TCPMasterProperties(String defaultIp, int defaultPort , List<String> ips, List<Integer> ports) {

        this.defaultIp = defaultIp;
        this.defaultPort = defaultPort;
        this.ips = ips;
        this.ports = ports;
    }

}
